package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.cache.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.permission.RequireLogin;
import cn.wolfcode.wolf2w.permission.UserParam;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("travels")
public class TravelController {


    @Autowired
    private ITravelService travelService;

    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private ITravelCommentService travelCommentService;


    @GetMapping("/query")
    private Object query(TravelQuery qo) {

        return JsonResult.success(travelService.query(qo));
    }

    @GetMapping("/input")
    private Object input(String id) {
        List<Destination> dests = destinationService.list();
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.hasLength(id)) {
            Travel tv = travelService.get(id);
            map.put("tv", tv);
        }
        map.put("dests", dests);
        return JsonResult.success(map);
    }

    //上传图片
    @RequestMapping("/contentImage")

    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception {
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName() + "\", \"size\": " + up.getSize()
                + ", \"state\": \"" + up.getState() + "\", \"type\": \"" + up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";
        result = result.replaceAll("\\\\", "\\\\");
        if (callback == null) {
            return result;
        } else {
            return "<script>" + callback + "(" + result + ")</script>";
        }
    }

    @RequireLogin
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel, HttpServletRequest request) {
        String token = request.getHeader("token");
        UserInfo user = userInfoRedisService.getUserByToken(token);
        travel.setUserId(user.getId());
        travelService.saveOrUpdate(travel);
        return JsonResult.success(travel.getId());
    }
    @GetMapping("/detail")
    private Object detail(String  id) {

        return JsonResult.success(travelService.get(id));
    }


    //利用注解区分解析器的使用类型
    @GetMapping("/getUser")
    public  Object getUser(@UserParam UserInfo userInfo){
      //获取当前登录用户对象
        return JsonResult.success(userInfo);
    }
    @GetMapping("/updateUser")
    public  Object updateUser(UserInfo userInfo){
      //修改用户对象
        return JsonResult.success(userInfo);
    }

    @RequireLogin
    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment travelComment,@UserParam UserInfo userInfo) {
        BeanUtils.copyProperties(userInfo,travelComment);
        travelComment.setUserId(userInfo.getId());
        travelCommentService.save(travelComment);
        return JsonResult.success();
    }

    @GetMapping("/comments")
    public Object comments(TravelComment travelComment) {
        List<TravelComment> list = travelCommentService.list();
        return JsonResult.success(list);
    }
}
